package com.bw.wxy.fragment;

import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.bw.wxy.R;
import com.bw.wxy.factroy.HttpCode;
import com.bw.wxy.mvp.base.view.BaseFragment;
import com.bw.wxy.mvp.contract.LREContract;
import com.bw.wxy.mvp.di.DaggerLREComponent;
import com.bw.wxy.mvp.di.LREModules;
import com.bw.wxy.mvp.model.entity.BaseEntity;
import com.bw.wxy.mvp.model.entity.VideoNewsEntity;
import com.bw.wxy.mvp.presenter.LREPresenter;
import com.bw.wxy.mvp.view.video.adapter.SmallVideoAdapter;
import com.bw.wxy.widget.VideoPartingLine;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Fragment_Small_Video extends BaseFragment<LREPresenter> implements LREContract.ILREView, OnRefreshLoadMoreListener {
    private SmartRefreshLayout smallSmart;
    private RecyclerView smallRv;
    private SmallVideoAdapter adapter;
    private GridLayoutManager manager;

    @Override
    public int bindLayout() {
        return R.layout.fragment__small__video;
    }

    @Override
    public void initView() {
        smallSmart = f(R.id.small_smart);
        smallSmart.setEnableRefresh(true);
        smallSmart.setEnableLoadMore(true);
        smallSmart.setRefreshFooter(new ClassicsFooter(getActivity()));
        smallSmart.setRefreshHeader(new ClassicsHeader(getActivity()));
        smallSmart.setOnRefreshListener(this);
        smallRv = f(R.id.small_rv);
        manager = new GridLayoutManager(getContext(),2);
        manager.setOrientation(GridLayoutManager.VERTICAL);
        smallRv.setLayoutManager(manager);
        smallRv.addItemDecoration(new VideoPartingLine(getContext()));
        adapter = new SmallVideoAdapter(new ArrayList<>());
        smallRv.setAdapter(adapter);
    }

    @Override
    public void initData() {
        Map<String,Object> map = new HashMap<>();
        map.put("news_type",0);
        map.put("code", HttpCode.VIDEONEWSCODE);
        p.requestAll(map);
    }

    @Override
    public void inject() {
        DaggerLREComponent.builder().lREModules(new LREModules(this)).build()
                .lreSmallVideo(this);
    }

    @Override
    public void refreshAll(BaseEntity baseEntity) {
        VideoNewsEntity videoNewsEntity = (VideoNewsEntity) baseEntity;
        if (videoNewsEntity.getValues()==null){
            Toast.makeText(getActivity(), "请求出错", Toast.LENGTH_SHORT).show();
            return;
        }
        adapter.setNewData(videoNewsEntity.getValues());
    }

    @Override
    public void refreshRecyclerView(BaseEntity baseEntity) {
        VideoNewsEntity videoNewsEntity = (VideoNewsEntity) baseEntity;
        if (videoNewsEntity.getValues()==null){
            Toast.makeText(getActivity(), "请求出错", Toast.LENGTH_SHORT).show();
            return;
        }
        adapter.setNewData(videoNewsEntity.getValues());
    }

    @Override
    public void loadMoreRecyclerView(BaseEntity baseEntity) {
        VideoNewsEntity videoNewsEntity = (VideoNewsEntity) baseEntity;
        if (videoNewsEntity.getValues()==null){
            Toast.makeText(getActivity(), "最后一页", Toast.LENGTH_SHORT).show();
            return;
        }
        adapter.addData(videoNewsEntity.getValues());
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();
        Map<String,Object>map = new HashMap<>();
        map.put("news_type",0);
        map.put("code", HttpCode.VIDEONEWSCODE);
        p.requestLoad(map);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();
        Map<String,Object>map = new HashMap<>();
        map.put("news_type",0);
        map.put("code", HttpCode.VIDEONEWSCODE);
        p.requestRefresh(map);
    }
}